package org.sharelife.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.sharelife.commons.Constants;
import org.sharelife.entity.User;
import org.sharelife.service.UserService;
import org.sharelife.util.DateUtils;
import org.sharelife.util.ImageBean;
import org.sharelife.util.ImageCutter;
import org.sharelife.util.PageBean;
import org.sharelife.util.UploadUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport {

	private User user;

	private UserService userService;

	private int regStep;

	private ImageBean imageBean;

	private String captcha;

	private String year = "1991";

	private String month = "01";

	private String day = "01";

	private PageBean pageBean;

	private int pageSize = 10;

	private int page = 1;

	private String keyWord;

	/**
	 * @Description 用户登录
	 * @return
	 * @throws Exception
	 */
	public String signin() throws Exception {
		try {
			user = userService.signin(user.getUserName(), user.getPassword());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (null != user) {
			ServletActionContext.getRequest().getSession().setAttribute(
					"loginUser", user);
			return "signin";
		}
		return INPUT;
	}

	/**
	 * @Description 用户注册
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception {
		try {
			user.setBirthDay(DateUtils.formatBirthDay(year, month, day));
			if (userService.register(user, regStep)) {
				user = userService.findByName(user.getUserName());
				ActionContext.getContext().getSession().put("loginUser", user);
				if (regStep == 1) {
					return "reg_step2";
				} else {
					return "reg_step3";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "reg_failure";
	}

	/**
	 * @Description 检测用户名是否存在
	 * @return
	 * @throws Exception
	 */
	public String checkUserName() throws Exception {
		try {
			user = userService.findByName(user.getUserName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (null != user) {
			ServletActionContext.getResponse().getWriter().write("false");
		} else {
			ServletActionContext.getResponse().getWriter().write("true");
		}
		return NONE;
	}

	/**
	 * @Description 检测验证码是否一致
	 * @return
	 * @throws Exception
	 */
	public String checkCaptcha() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		String genCode = (String) session.getAttribute(session.getId());
		if (captcha.equalsIgnoreCase(genCode)) {
			ServletActionContext.getResponse().getWriter().write("true");
		} else {
			ServletActionContext.getResponse().getWriter().write("false");
		}
		return NONE;
	}

	/**
	 * @Description 检测相册空间是否已被使用
	 * @return
	 * @throws Exception
	 */
	public String checkAlbum() throws Exception {
		try {
			user = userService.findByAlbum(user.getAlbum());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (null != user) {
			ServletActionContext.getResponse().getWriter().write("false");
		} else {
			ServletActionContext.getResponse().getWriter().write("true");
		}
		return NONE;
	}

	/**
	 * @Description 检测邮箱是否存在
	 * @return
	 * @throws Exception
	 */
	public String checkEmail() throws Exception {
		try {
			user = userService.findByEmail(user.getEmail());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (null != user) {
			ServletActionContext.getResponse().getWriter().write("false");
		} else {
			ServletActionContext.getResponse().getWriter().write("true");
		}
		return NONE;
	}

	/**
	 * @Description 自定义个人头像
	 * @return
	 * @throws Exception
	 */
	public String modifyPhoto() throws Exception {
		try {
			if (ImageCutter.saveImage(imageBean, user.getAlbum())) {
				user = userService.modifyPhoto(UploadUtils
						.photoStickerURL(imageBean.getUrl()), user.getId());
				ActionContext.getContext().getSession().put(
						Constants.LOGINUSER, user);
				return NONE;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "input";
	}

	/**
	 * @Description 管理员的登录
	 * @return
	 * @throws Exception
	 */
	public String adminLogin() throws Exception {
		try {
			user = userService.adminLogin(user.getUserName(), user.getPassword());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (user != null) {
			ActionContext.getContext().getSession().put(Constants.LOGINUSER, user);
			return "adminLogin_success";
		}

		return "adminLogin_failure";
	}

	/**
	 * @Description 添加管理员
	 * @return
	 * @throws Exception
	 */
	public String modifyToAdmin() throws Exception {
		if (userService.modifyToAdmin(user.getId())) {
			return "modifyToAdmin";
		}
		return "add_failure";
	}
	
	/**
	 * @Description 后台管理员退出
	 * @return
	 * @throws Exception
	 */
	public String AdminLogout() throws Exception {
		try {
			ServletActionContext.getRequest().getSession().removeAttribute(
					"Logined");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return " AdminLogout_success";
	}

	/**
	 * @Description 根据用户id删除用户
	 * @return
	 * @throws Exception
	 */
	public String delteteById() throws Exception {
		try {
			userService.deleteById(user.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delteteById_success";
	}

	/**
	 * @Description 查找所有用户并分页显示
	 * @return
	 * @throws Exception
	 */
	public String findAll() throws Exception {
		try {
			pageBean = userService.queryForPage(pageSize, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findAll_success";
	}

	/**
	 * @Description 前台根据用户id获取用户信息
	 * @return
	 * @throws Exception
	 */
	public String findById() throws Exception {
		try {
			user = userService.findById(user.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findById_success";
	}

	/**
	 * @Description 后台根据用户id获取用户信息
	 * @return
	 * @throws Exception
	 */
	public String findInfoById() throws Exception {
		try {
			user = userService.findById(user.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findInfoById_success";
	}

	/**
	 * @Description 前台用户退出
	 * @return
	 * @throws Exception
	 */
	public String logout() throws Exception {
		try {
			ServletActionContext.getRequest().getSession().removeAttribute(
					Constants.LOGINUSER);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "logout_success";
	}

	/**
	 * @Description 修改用户信息
	 * @return
	 * @throws Exception
	 */
	public String modify() throws Exception {
		try {
			user.setBirthDay(DateUtils.formatBirthDay(year, month, day));
			userService.modify(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "modifyById_success";
	}

	/**
	 * @Description 根据关键字分页查询
	 * @return
	 * @throws Exception
	 */
	public String queryForPageByKeyWords() throws Exception {
		try {
			pageBean = userService.queryForPageByKeyWords(keyWord, pageSize,
					page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "queryForPageByKeyWords_success";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public int getRegStep() {
		return regStep;
	}

	public void setRegStep(int regStep) {
		this.regStep = regStep;
	}

	public ImageBean getImageBean() {
		return imageBean;
	}

	public void setImageBean(ImageBean imageBean) {
		this.imageBean = imageBean;
	}

	public String getCaptcha() {
		return captcha;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
}